﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ApplicationConfiguration.cs" company="TechBits">
//   Copyright (c) TechBits. All rights reserved.
// </copyright>
// <author>
//   Leandro G. Vacirca.
// </author>
// <summary>
//   Defines the ApplicationConfiguration type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace IntelliShortCut.Application
{
    using System;
    using System.IO;
    using System.Windows.Forms;
    using System.Xml.Serialization;
    
    using Data;

    /// <summary>
    /// Configuration values of the application.
    /// </summary>
    public class ApplicationConfiguration
    {
        #region "Constructors"

        /// <summary>
        /// Initializes static members of the <see cref="ApplicationConfiguration"/> class. 
        /// </summary>
        static ApplicationConfiguration()
        {
            Settings = new AppSettingsData();
            LoadSettings();
        }

        #endregion

        #region "Events"

        /// <summary>
        /// AppSettings on changed event.
        /// </summary>
        public static event EventHandler OnAppSettingsChanged;

        #endregion

        #region "Public Properties"

        /// <summary>
        /// Gets or sets Settings.
        /// </summary>
        public static AppSettingsData Settings { get; set; }

        /// <summary>
        /// Gets the application name.
        /// </summary>
        public static string Name
        {
            get
            {
                return Application.ProductName;
            }
        }

        /// <summary>
        /// Gets the application path, not including the executable name.
        /// </summary>
        public static string Path
        {
            get
            {
                return Application.StartupPath;
            }
        }

        /// <summary>
        /// Gets the application path, including the executable name.
        /// </summary>
        public static string FullPath
        {
            get
            {
                return Application.ExecutablePath;
            }
        }

        /// <summary>
        /// Gets MainWindowName.
        /// </summary>
        public static string MainWindowName
        {
            get
            {
                return "IntelliShortCut.exe";
            }
        }

        /// <summary>
        /// Gets the XMLDataFile name.
        /// </summary>
        public static string XMLDataFileName
        {
            get
            {
                return "IntelliShortcutDB.xml";
            }
        }

        /// <summary>
        /// Gets XMLDataBackupFileName.
        /// </summary>
        public static string XMLDataBackupFileName
        {
            get
            {
                return "IntelliShortcutDB-Backup.xml";
            }
        }

        /// <summary>
        /// Gets XMLSettingsFileName.
        /// </summary>
        public static string XMLSettingsFileName
        {
            get
            {
                return "IntelliShortcutSettings.xml";                
            }            
        }

        /// <summary>
        /// Gets XMLDataBackupFile.
        /// </summary>
        public static string XMLDataBackupFile
        {
            get
            {
                return System.IO.Path.Combine(System.IO.Path.GetDirectoryName(XMLDataFile), XMLDataBackupFileName);
            }
        }

        /// <summary>
        /// Gets the XMLDataFile full path.
        /// </summary>
        public static string XMLDataFile
        {
            get
            {
                var vendorPath = System.IO.Path.Combine("TechBits", "IntelliShortCut");
                var appDataPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), vendorPath);
                
                if (!Directory.Exists(appDataPath))
                {
                    Directory.CreateDirectory(appDataPath);
                }

                return System.IO.Path.Combine(appDataPath, XMLDataFileName);
            }
        }

        /// <summary>
        /// Gets XMLSettingsFile full path.
        /// </summary>
        public static string XMLSettingsFile
        {
            get
            {
                var vendorPath = System.IO.Path.Combine("TechBits", "IntelliShortCut");
                var appSettingsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), vendorPath);

                if (!Directory.Exists(appSettingsPath))
                {
                    Directory.CreateDirectory(appSettingsPath);
                }

                return System.IO.Path.Combine(appSettingsPath, XMLSettingsFileName);
            }
        }

        /// <summary>
        /// Gets the max quantity of items to show in the last used shortcuts.
        /// </summary>
        public static int LastUsedItemsCount
        {
            get
            {
                return 5;
            }
        }

        /// <summary>
        /// Gets the max quantity of items to show in the most used shortcuts.
        /// </summary>
        public static int MostUsedItemsCount
        {
            get
            {
                return 8;
            }
        }

        #endregion

        #region "Public Methods"

        /// <summary>
        /// Saves the shortcuts data to the xml data file.
        /// </summary>
        public static void SaveSettings()
        {
            var xmlSerializer = new XmlSerializer(typeof(AppSettingsData));
            using (var streamWriter = new StreamWriter(XMLSettingsFile))
            {
                xmlSerializer.Serialize(streamWriter, Settings);
            }

            InvokeOnAppSettingsChanged(new EventArgs());
        }

        #endregion

        #region "Private Methods"

        /// <summary>
        /// Loads the shortcuts data from the xml data file.
        /// </summary>
        private static void LoadSettings()
        {
            try
            {
                var xmlSerializer = new XmlSerializer(typeof(AppSettingsData));
                using (var streamReader = new StreamReader(XMLSettingsFile))
                {
                    Settings = (AppSettingsData)xmlSerializer.Deserialize(streamReader);
                }
            }
            catch (Exception)
            {
                DefaultSettings(Settings);
            }            
        }

        /// <summary>
        /// Sets the defualt settings.
        /// </summary>
        /// <param name="settingsData">The settings Data.</param>
        private static void DefaultSettings(AppSettingsData settingsData)
        {
            settingsData.AutoStartWithWindows = true;
            settingsData.MainWindowOpacity = 85;
            settingsData.FirstRun = true;
            settingsData.ShortcutsSavedCount = 0;
            settingsData.BackUpPeriod = (int)new TimeSpan(0, 0, 10, 0).TotalMilliseconds;

            SaveSettings();
        }

        /// <summary>
        /// Invokes the event.
        /// </summary>
        /// <param name="e">The event parameters.</param>
        private static void InvokeOnAppSettingsChanged(EventArgs e)
        {
            EventHandler handler = OnAppSettingsChanged;
            if (handler != null)
            {                
                handler("SaveSettings", e);
            }
        }

        #endregion
    }
}
